package com.zyd.bootpage;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.EdgeEffectCompat;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

//引导页
public class FirstLauncherActivity extends AppCompatActivity {

    private ViewPager viewPager;
    //图片数组
    private int[] images = new int[]{R.mipmap.bg_launcher_one, R.mipmap.bg_launcher_two};
    //显示图片View
    private List<View> mImages = new ArrayList<>();
    //圆点View
    private List<ImageView> tips = new ArrayList<>();

    private ViewGroup group;
    private EdgeEffectCompat rightEdge;
    private TextView tvGotoMain;

    private ImageView dot1;
    private ImageView dot2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_launcher);
        group = findViewById(R.id.viewGroup);
        viewPager = findViewById(R.id.viewPager);
        tvGotoMain = findViewById(R.id.tv_goto_main);
        dot1 = findViewById(R.id.iv_01);
        dot2 = findViewById(R.id.iv_02);

        try {
            Field rightEdgeField = viewPager.getClass().getDeclaredField("mRightEdge");
            if (rightEdgeField != null) {
                rightEdgeField.setAccessible(true);
                rightEdge = (EdgeEffectCompat) rightEdgeField.get(viewPager);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //图片装到数组中
        for (int i = 0; i < images.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(images[i]);
            mImages.add(imageView);
        }
        //将圆点加到ViewGroup中
        tips.add(dot1);
        tips.add(dot2);
        // 加载图片资源可以用些方式
//        for (int i = 0; i < images.length; i++) {
//            ImageView imageView = new ImageView(this);
//            imageView.setLayoutParams(new ViewGroup.LayoutParams(10, 10));
//            if (i == 0) {
//                imageView.setImageResource(R.drawable.dot2);
//            } else {
//                imageView.setImageResource(R.drawable.dot);
//            }
//            tips.add(imageView);
//            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
//                    ViewGroup.LayoutParams.WRAP_CONTENT));
//            layoutParams.leftMargin = 10;
//            layoutParams.rightMargin = 10;
//            group.addView(imageView, layoutParams);
//        }
        //设置Adapter
        viewPager.setAdapter(new PreviewImageAdapter());
        viewPager.setOffscreenPageLimit(2);
        //设置监听，主要是设置圆点的背景
        viewPager.addOnPageChangeListener(onPageChangeListener);

        tvGotoMain.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_goto_main:
                    gotoMain();
                    break;
            }
        }
    };
    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == images.length - 1) {//最后一页
                tvGotoMain.setVisibility(View.VISIBLE);
            } else {
                tvGotoMain.setVisibility(View.INVISIBLE);
            }
            setImageBackground(position);
        }


        @Override
        public void onPageScrollStateChanged(int state) {
            if (rightEdge != null && !rightEdge.isFinished()) {
                //到最后一张并且继续拖动，出现蓝色限制边条
                gotoMain();
            }

        }
    };

    private void setImageBackground(int selectItems) {
        for (int i = 0; i < tips.size(); i++) {
            if (i == selectItems) {
                if (i == 0) {
                    tips.get(i).setBackgroundResource(R.drawable.dot2);
                } else if (i == 1) {
                    tips.get(i).setBackgroundResource(R.drawable.dot2);
                }
            } else {
                tips.get(i).setBackgroundResource(R.drawable.dot);
            }
        }
    }

    public class PreviewImageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImages.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//            super.destroyItem(container, position, object);
            if (position < mImages.size()) {
                container.removeView(mImages.get(position));
            }
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(mImages.get(position));
            return mImages.get(position);
        }
    }

    /**
     * 跳转到首页
     */
    private void gotoMain() {
        isFirstLauncherBoolean();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 第一次启动完成 ，设置为true
     */
    public void isFirstLauncherBoolean() {
        SharedPreferences sp = getSharedPreferences("guide", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean(LauncherActivity.FIRST_LAUNCHER, true);
        edit.commit();
    }
}